package cn.com.blueInfo.bpm.template.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.template.entity.EventAction;
import cn.com.blueInfo.bpm.template.enums.EventActionType;
import cn.com.blueInfo.bpm.template.mapper.EventActionMapper;
import cn.com.blueInfo.bpm.template.repository.EventActionRepository;
import cn.com.blueInfo.bpm.template.request.EventActionDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 事件动作表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:30:44
 */
@Log4j2
@Service
public class EventActionRepositoryImpl
        extends ServiceImpl<EventActionMapper, EventAction>
        implements EventActionRepository, BaseRepository<EventAction, EventActionDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增事件动作
     * @param eventAction 事件动作参数
     */
    @Override
    public Integer createEventAction(EventAction eventAction) {
        return sqlExecuteResult(save(eventAction));
    }

    /**
     * 批量新增事件动作
     * @param eventActionList 事件动作参数列表
     */
    @Override
    public Integer batchCreateEventAction(List<EventAction> eventActionList) {
        return sqlExecuteResult(saveBatch(eventActionList), eventActionList.size());
    }

    /**
     * 删除事件动作
     * @param eventActionId 事件动作ID
     */
    @Override
    public Integer deleteEventAction(Long eventActionId) {
        EventAction eventAction = new EventAction();
        eventAction.setId(eventActionId);
        eventAction.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(eventAction));
    }

    /**
     * 批量删除事件动作
     * @param eventActionIdList 事件动作ID列表
     */
    @Override
    public Integer batchDeleteEventAction(List<Long> eventActionIdList) {
        List<EventAction> eventActionList = new ArrayList<>();
        for (Long eventActionId : eventActionIdList) {
            EventAction eventAction = new EventAction();
            eventAction.setId(eventActionId);
            eventAction.setDeleted(BaseDeleted.DELETED);
            eventActionList.add(eventAction);
        }
        return sqlExecuteResult(removeBatchByIds(eventActionList), eventActionList.size());
    }

    /**
     * 更新部分事件动作
     * @param eventAction 事件动作参数
     */
    @Override
    public Integer updateEventActionPartial(EventAction eventAction) {
        validateUpdateId(eventAction.getId());
        LambdaUpdateWrapper<EventAction> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(EventAction::getId, eventAction.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部事件动作
     * @param eventAction 事件动作参数
     */
    @Override
    public Integer updateEventAction(EventAction eventAction) {
        validateUpdateId(eventAction.getId());
        return sqlExecuteResult(updateById(eventAction));
    }

    /**
     * 查询一条事件动作
     * @param eventActionId 事件动作ID
     */
    @Override
    public EventAction getEventActionById(Long eventActionId) {
        LambdaQueryWrapper<EventAction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EventAction::getId, eventActionId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询事件动作
     * @param eventActionQueryDTO 事件动作参数
     */
    @Override
    public Page<EventAction> getEventActionPage(EventActionDTO.QueryDTO eventActionQueryDTO) {
        Page<EventAction> eventActionPage = new Page<>(
                eventActionQueryDTO.getPage(), eventActionQueryDTO.getSize());
        LambdaQueryWrapper<EventAction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, eventActionQueryDTO);

        BaseDeleted deleted = eventActionQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(eventActionPage, queryWrapper);
        } else {
            return page(eventActionPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 事件动作 数量
     * @param eventActionQueryDTO 事件动作参数
     */
    @Override
    public Long getEventActionCount(EventActionDTO.QueryDTO eventActionQueryDTO) {
        LambdaQueryWrapper<EventAction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, eventActionQueryDTO);

        BaseDeleted deleted = eventActionQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 事件动作
     * @param eventActionQueryDTO 事件动作参数
     */
    @Override
    public List<EventAction> getEventActionList(EventActionDTO.QueryDTO eventActionQueryDTO) {
        LambdaQueryWrapper<EventAction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, eventActionQueryDTO);

        BaseDeleted deleted = eventActionQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<EventAction> queryWrapper,
                              EventActionDTO.QueryDTO eventActionQueryDTO) {

        Long eventId = eventActionQueryDTO.getEventId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(eventId), EventAction::getEventId, eventId);

        EventActionType actionType = eventActionQueryDTO.getActionType();
        queryWrapper.eq(ObjectUtils.isNotEmpty(actionType), EventAction::getActionType, actionType);

        String actionParam = eventActionQueryDTO.getActionParam();
        queryWrapper.eq(StringUtils.isNotBlank(actionParam), EventAction::getActionParam, actionParam);

        addQueryWrapper(queryWrapper, eventActionQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<EventAction, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                EventAction::getOrderNumber,
                EventAction::getTenantId,
                EventAction::getApplicationId,
                EventAction::getStatus,
                EventAction::getDeleted,
                EventAction::getDeleteUserId,
                EventAction::getDeleteTime,
                EventAction::getRemark,
                EventAction::getCreateUserId,
                EventAction::getCreateTime,
                EventAction::getUpdateUserId,
                EventAction::getUpdateTime,
                EventAction::getExtend1,
                EventAction::getExtend2,
                EventAction::getExtend3);
    }

}
